for N = 59:60
    %初始化参数
    %N = 31;
    d = 0.88;
    %设置初始构型
    config_Nless = cell(1,N-1);
    fname = sprintf('N=%d.txt',N-1);
    data = load(['.\final_results\',fname]);
    for i=1:(N-1)
        config_Nless{i} = data(i+N,:);
    end
    config0 = particle_addition(config_Nless);
%     data = load('.\pot2\N=55-1.txt');
%     config0 = cell(1,N);
%     for i=1:N
%         config0{i} = data(i+N+1,:);
%     end
    
    N0 = length(config0);
    x0 = zeros(1,N0);
    y0 = zeros(1,N0);
    z0 = zeros(1,N0);
    for j=1:N0
        x0(j) = config0{j}(1);
        y0(j) = config0{j}(2);
        z0(j) = config0{j}(3);
    end
    limit = [min(x0),max(x0),min(y0),max(y0),min(z0),max(z0)];

    config_former = config0;
    en_former  = en_cal(config_former);
    
    %记录5次SA中最好的结果（取最小能量最小的那个），记录该次SA中SA最后得到的结果（{1}构型{2}能量）以及过程中能量最小的结果（{3}构型{4}能量）
    EN_MIN = cell(1,4);
    EN_MIN{1} = config_former;
    EN_MIN{2} = en_former;
    EN_MIN{3} = config_former;
    EN_MIN{4} = en_former;
    
    for p = 1:5
        %en_min记录粗糙退火过程中目前最小能量对应的构型{1}和能量{2}
        en_min = cell(1,2);
        en_min{1} = config_former;
        en_min{2} = en_former;

        T_max = 2;
        T_min = 1 * 1e-3;
        lambda = 0.90;
        step = N*100;
        dr = 0.8;
        choice = 0;
        T = T_max;
        while T > T_min
%             if(dr > 0.05)
%                 step = N * 300;
%             else
%                 step = N * 100;
%             end
            for i = 1:step
                [config,en] = change_config(config_former, en_former, limit,dr,choice);
                if(en < en_former)
                    config_former = config;
                    en_former = en;
                    if(en < en_min{2})
                        en_min{1} = config;
                        en_min{2} = en;
                    end
                else
                    rand_num = rand;
                    factor = exp((en_former-en)/T);
                    if(rand_num < factor)
                        config_former = config;
                        en_former = en;
                    else

                    end
                end
            end
            T = lambda * T;
            dr = lambda^(5/7) * dr;

        end
        if(en_min{2} < EN_MIN{4})
            EN_MIN{1} = config_former;
            EN_MIN{2} = en_former;
            EN_MIN{3} = en_min{1};
            EN_MIN{4} = en_min{2};
        end
    end

    %写.txt文件，存数据
    fname = sprintf('N=%d.txt',N);
    fid = fopen(['.\final_results\',fname],'w');
    for k = 1:N
        for m = 1:3
            fprintf(fid, '%.8f\t', EN_MIN{1}{k}(m));
        end
        fprintf(fid, '\n');
    end
    for m = 1:3
        fprintf(fid, '%.8f\t', EN_MIN{2});
    end
    fprintf(fid, '\n');
    for k = 1:N
        for m = 1:3
            fprintf(fid, '%.8f\t', EN_MIN{3}{k}(m));
        end
        fprintf(fid, '\n');
    end
    for m = 1:3
        fprintf(fid, '%.8f\t', EN_MIN{4});
    end
    fclose(fid);

    %画图
%     x = zeros(1,N);
%     y = zeros(1,N);
%     z = zeros(1,N);
% 
%     for j=1:N
%         x(j) = config_former{j}(1);
%         y(j) = config_former{j}(2);
%         z(j) = config_former{j}(3);
%     end
% 
%     scatter3(x,y,z ,'filled');
%     xlabel('x');
%     ylabel('y');
%     zlabel('z');
%     set(gca,'GridLineStyle','--');
%     set(gca,'GridAlpha',0.3);

end
